package org.mat.framework.service.admin;

import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;

import java.util.Properties;

/**
 * @ClassName: SpringCloudAdminEnvironmentPostProcessor
 * @Date: 2021/9/1
 * @author: sunxinhe
 * @Version: 1.0
 * @Description: TODO
 *   server.servlet.context-path
 */
public class SpringCloudAdminEnvironmentPostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        Properties props = new Properties();
        boolean needOverwriteProperties = false;

        String servletContextPath = environment.getProperty("server.servlet.context-path");
        String nacosMetadataMnagementContextPath = environment.getProperty("spring.cloud.nacos.discovery.metadata.management.context-path");
        if (StringUtils.isNotBlank(servletContextPath)&&StringUtils.isBlank(nacosMetadataMnagementContextPath)) {
            needOverwriteProperties = true;
            props.put("spring.cloud.nacos.discovery.metadata.management.context-path", servletContextPath+"/actuator");
        }

        if (needOverwriteProperties) {
            environment.getPropertySources().addFirst(new PropertiesPropertySource("mat_ext_spring_cloud_admin_properties", props));
        }

    }
}
